package com.zbkj.crmeb.store.controller;

import java.util.Set;

import javax.validation.constraints.NotEmpty;

import com.common.CommonPage;
import com.github.pagehelper.util.StringUtil;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.request.StoreBrandAuditRequest;
import com.zbkj.crmeb.store.response.StoreBrandAuditResponse;
import com.zbkj.crmeb.store.service.StoreBrandAuditService;
import com.zbkj.crmeb.store.service.StoreProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.enums.BrandTypeEnum;
import com.zbkj.crmeb.store.dto.StoreBrandDTO;
import com.zbkj.crmeb.store.model.StoreBrand;
import com.zbkj.crmeb.store.request.StoreBrandRequest;
import com.zbkj.crmeb.store.service.StoreBrandService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 品牌管理
 *
 * @author daigb
 * @Description:
 * @date 2021-04-13
 */
@RestController
@RequestMapping("api/admin/store/brand")
@Api(tags = "品牌管理")
public class StoreBrandController {
    @Autowired
    StoreBrandService storeBrandService;
    @Autowired
    StoreProductService storeProductService;
    @Autowired
    StoreBrandAuditService storeBrandAuditService;

    /**
     * 列表
     *
     * @param @param  type 类型required(1: 寻物  2: eur 3: buyer)
     * @param @return
     * @return CommonResult<Object>
     * @throws
     * @Description:
     * @author daigb
     */
    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public CommonResult<Object> listEntity(@RequestParam(required = true) Integer type) {
        return CommonResult.success(storeBrandService.list(Wrappers.<StoreBrand>lambdaQuery()
                .eq(StoreBrand::getType, BrandTypeEnum.valueOf(type))
                .eq(StoreBrand::getStatus, true)
                .eq(StoreBrand::getCreateUserId, 0)
                .orderByAsc(StoreBrand::getLetter))
        );
    }

    /**
     * 分页查询
     *
     * @param @param  storeBrandRequest
     * @param @return
     * @return CommonResult<Object>
     * @throws
     * @Description:
     * @author daigb
     */
    @ApiOperation(value = "分页")
    @PostMapping("/page")
    public CommonResult<Object> pageEntity(@RequestBody StoreBrandRequest storeBrandRequest) {
        return CommonResult.success(storeBrandService.pageEntity(storeBrandRequest));
    }

    /**
     * 通过id查询
     *
     * @param @param  id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     * @Description:
     * @author daigb
     */
    @ApiOperation(value = "通过id获取")
    @GetMapping("/{id}")
    public CommonResult<Object> getEntity(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id) {
        return CommonResult.success(storeBrandService.getEntity(id));
    }

    /**
     * 新增
     *
     * @param @param  storeBrandDTO
     * @param @return
     * @return CommonResult<Object>
     * @throws
     * @Description:
     * @author daigb
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public CommonResult<Object> save(@RequestBody StoreBrandDTO storeBrandDTO) {
        if (null == storeBrandDTO.getType()) {
            return CommonResult.validateFailed("品牌类型不能为空");
        }
        if (BrandTypeEnum.XUNWU != BrandTypeEnum.valueOf(storeBrandDTO.getType()) && null == storeBrandDTO.getSuperId()) {
            return CommonResult.validateFailed("关联寻物品牌不能为空");
        }
        if (StringUtil.isEmpty(storeBrandDTO.getEnName())) {
            return CommonResult.validateFailed("品牌名称不能为空");
        }
        StoreBrand check = storeBrandService.getBrandName(storeBrandDTO.getEnName(), 0);
        if (null != check) {
            return CommonResult.validateFailed("品牌名称系统已存在");
        }
        storeBrandDTO.setIdentityType(0);
        storeBrandDTO.setCreateUserId(0);
        return CommonResult.success(storeBrandService.saveEntity(storeBrandDTO));
    }

    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public CommonResult<Object> update(@RequestBody StoreBrandDTO storeBrandDTO) {
        if (null == storeBrandDTO.getId()) {
            return CommonResult.validateFailed("品牌ID不能为空");
        }
        if (null == storeBrandDTO.getType()) {
            return CommonResult.validateFailed("品牌类型不能为空");
        }
        if (BrandTypeEnum.XUNWU != BrandTypeEnum.valueOf(storeBrandDTO.getType()) && null == storeBrandDTO.getSuperId()) {
            return CommonResult.validateFailed("关联寻物品牌不能为空");
        }
        if (StringUtil.isEmpty(storeBrandDTO.getEnName())) {
            return CommonResult.validateFailed("品牌名称不能为空");
        }
        StoreBrand check = storeBrandService.getBrandName(storeBrandDTO.getEnName(), 0);
        if (null != check && !check.getId().equals(storeBrandDTO.getId())) {
            return CommonResult.validateFailed("品牌名称系统已存在");
        }
        return CommonResult.success(storeBrandService.updateEntity(storeBrandDTO));
    }

    @PutMapping("/is-enabled/{id}/update/{status}")
    @ApiOperation("禁用或启用")
    public CommonResult<Object> enabled(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id, @ApiParam(value = "启用状态 1启用 0 禁用", required = true) @PathVariable("status") Integer status) {
        return CommonResult.success(storeBrandService.update(Wrappers.<StoreBrand>lambdaUpdate()
                .eq(StoreBrand::getId, id)
                .set(StoreBrand::getStatus, status)
        ));
    }

    /**
     * 根据id删除
     *
     * @param @param  id
     * @param @return
     * @return CommonResult<Object>
     * @THROWS
     * @Description:
     * @author daigb
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    public CommonResult<Object> del(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id) {
        int count = storeProductService.count(Wrappers.lambdaQuery(StoreProduct.class).eq(StoreProduct::getBrandId, id).eq(StoreProduct::getIsDel, 0));
        if (count > 0) {
            return CommonResult.validateFailed("该品牌下有商品,删除失败!");
        }
        return CommonResult.success(storeBrandService.removeById(id));
    }

    /**
     * 批量删除
     *
     * @param @param  ids
     * @param @return
     * @return CommonResult<Object>
     * @throws
     * @Description:
     * @author daigb
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/remove")
    public CommonResult<Object> delBatch(@NotEmpty(message = "id不能为空") @RequestBody Set<Integer> ids) {
        return CommonResult.success(storeBrandService.removeByIds(ids));
    }

    @ApiOperation(value = "查询品牌审核列表")
    @GetMapping("/audit/list")
    public CommonResult<CommonPage<StoreBrandAuditResponse>> getList(StoreBrandAuditRequest request) {
        return CommonResult.success(storeBrandAuditService.getList(request));
    }

    @ApiOperation(value = "驳回品牌申请")
    @PutMapping("/reject/{id}")
    public CommonResult<Boolean> rejectBrand(@PathVariable Integer id) {
        return CommonResult.success(storeBrandAuditService.rejectBrand(id));
    }

}
